<?php 
/***
	Jjson.1.0.php
	name:xiaojia
	email:iatt@qq.com
	qq:273142650
	time:2011.9.22 16:35
***/

	class Jjson 
	{	
		static function json($array, $json_name = '')
		{
			
			$php_version = preg_replace('/\./' , '', phpversion());
			
			if ($php_version > 520) {
				return "$json_name = " . json_encode($array);
			}
			
			$json[] = self::analytical_json_0($array);
			
			$json = implode(',', $json);
			
			return $json_name . " = { $json } ";
		}
		
		static function analytical_json_0($array)
		{
			
			foreach ($array as $key => $v) {
				if (is_array($v)) {
					$json[] = self::analytical_json_1($v, $key);
				} else {
					$json[] = "'$key':'$v'";
				}
			}
			
			$json = implode(',', $json);
			
			return $json;
			
		}
		
		static function analytical_json_1($array, $json_name)
		{
						
			foreach ($array as $key => $v) {
				if (is_array($v)) {
					$json_2[] = self::analytical_json_1($v, $key);
				} else {
					$json[] = "'$key':'$v'";
				}
			}
			
			if (!empty($json_2)) {
				$json = "'$json_name' : [" . implode(',', $json_2) . ']';
			} else {
				$json = "'$json_name' : {" . implode(',', $json) . '}';
			}
			
			return $json;
			
		}
		
		static function analytical_json_2($array, $json_name)
		{
			
			foreach ($array as $key => $v) {
				if (is_array($v)) {
					$json[] = self::analytical_json_0($array);
				} else {
					$json[] = "'$key':'$v'";
				}
			}
			
			$json = "{" . implode(',', $json) . '}';
			
			return $json;
			
		}		
		
	}//class end
?>